package com.hzzc.core.controller;

import com.hzzc.BizException;
import com.hzzc.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 统一异常处理
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result<String> defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        log.error("请求地址" + req.getRequestURL().toString() + "异常！", e);
        if (e instanceof BizException) {
            BizException bizException = (BizException) e;
            return Result.instance(bizException.getCode(), null, bizException.getMessage());
        }
        return Result.error(null, "系统异常");
    }

}
